package com.tvunetworks.center.device.service.impl;

import com.tvunetworks.center.common.model.param.ReceiverDeleteExtParam;
import com.tvunetworks.center.common.util.MyUtil;
import com.tvunetworks.center.device.mapper.ExternalReceiverRelationMapper;
import com.tvunetworks.center.device.model.ExternalReceiverRelation;
import com.tvunetworks.center.device.service.DeviceService;
import com.tvunetworks.center.device.service.ExternalReceiverRelationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: ExternalReceiverRelationServiceImpl
 * @author lebronchen
 * @create: 2020-01-09 14:02
 **/
@Service
@Slf4j
public class ExternalReceiverRelationServiceImpl implements ExternalReceiverRelationService {

    @Value("${gridPack}")
    private String gridPack;
    @Value("${gridSdi}")
    private String gridSdi;

    @Autowired
    private ExternalReceiverRelationMapper externalReceiverRelationMapper;
    @Autowired
    private DeviceService deviceService;

    @Override
    public void copyR(String oldPeerId, String newPeerId, String taskId) {
        // 复制 R
        // Old Relations
        List<ExternalReceiverRelation> oldRelations = externalReceiverRelationMapper.select(new ExternalReceiverRelation().setRId(oldPeerId));
        if (CollectionUtils.isEmpty(oldRelations)) {
            log.error("Start copyDevice ExternalReceiverRelation R oldRelations null taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return;
        }

        List<String> fullIds = externalReceiverRelationMapper.listFullIdsInPeerId1NotInPeerId2(oldPeerId, newPeerId);
        if (CollectionUtils.isEmpty(fullIds)) {
            log.error("Start copyDevice ExternalReceiverRelation R no changes need to copy  taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return;
        }
        List<ExternalReceiverRelation> copyList = new ArrayList<>();
        for (String fullId : fullIds) {
            ExternalReceiverRelation record = new ExternalReceiverRelation()
                    .setRId(newPeerId)
                    .setFullId(fullId)
                    .setId(MyUtil.getUUID());
            copyList.add(record);
        }
        externalReceiverRelationMapper.insertList(copyList);
        log.error("Start copyDevice ExternalReceiverRelation R need copy taskId:[{}] oldPeerId:[{}] newPeerId:[{}] copyList:[{}]", taskId, oldPeerId, newPeerId, copyList);


    }

    @Override
    public void receiverDeleteExt(ReceiverDeleteExtParam param) {
        String rid = param.getRid();
        List<String> peerIds = param.getPeerIds();
        if (StringUtils.isBlank(rid) || CollectionUtils.isEmpty(peerIds)) {
            return;
        }
        int result = externalReceiverRelationMapper.receiverDeleteExt(param);
        if (result == 0) {
            log.error("receiverDeleteExt db exist 0 record, do not need to preloadExtXToR param:[{}]", param);
            return;
        }
        log.error("receiverDeleteExt db exist [{}] record, need to preloadExtXToR param:[{}]", result, param);
        deviceService.preloadExtXToR(rid);
    }

}
